package cake.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import cake.util.FileUtil;
import cake.util.UploadPicture;

import cake.bean.Cake;
import cake.bean.Comment;
import cake.bean.Diy;
import cake.bean.Message;
import cake.bean.Order;
import cake.bean.OrderDetail;
import cake.bean.ShoppingCart;
import cake.bean.User;
import cake.bean.Manager;
import cake.dao.CommentDao;
import cake.dao.DiyDao;
import cake.dao.ManagerDao;
import cake.dao.CakeDao;
import cake.dao.MessageDao;
import cake.dao.OrderDao;
import cake.dao.OrderDetailDao;
import cake.dao.ShoppingCartDao;
import cake.dao.UserDao;

//�ͻ������࣬��Ҫ����ajax������ʵ���첽����
public class UserService {
	private UserDao dao;
	private HttpServletRequest req;
	private HttpServletResponse rep;

	public UserService(HttpServletRequest request, HttpServletResponse response) {
		this.rep = response;
		this.req = request;
		dao = new UserDao();
	}
	//�ͻ���½
	public void login() {
		String username = req.getParameter("name");	//��ȡ�����е��û���
		String password = req.getParameter("pwd");	//��ȡ�����е�����

		User user = dao.getUser(username, password);

		String result = "";

		if (user == null) {
			result = "false";
		} else {
			//��½�ɹ������ͻ����û����id���浽session��
			req.getSession().setAttribute("username", username);
			req.getSession().setAttribute("userid", user.getId());
			Cookie name_cookie = new Cookie("username",username); 
			name_cookie.setMaxAge(30*24*3600);
			name_cookie.setPath("/");
			rep.addCookie(name_cookie);
			Cookie id_cookie = new Cookie("userid",user.getId()+""); 
			id_cookie.setMaxAge(30*24*3600);
			id_cookie.setPath("/");
			rep.addCookie(id_cookie);
			result = "true";
		}
		try {
			//�����½���
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//����Ա��½
	public void adminLogin() {
		String username = req.getParameter("name");
		String password = req.getParameter("pwd");
		ManagerDao mDao=new ManagerDao();
		Manager user = mDao.getManager(username, password);

		String result = "";

		if (user == null) {
			result = "false";
		} else {
			req.getSession().setAttribute("managername", username);
			req.getSession().setAttribute("managerid", user.getId());
			result = "true";
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//�ͻ��˳�
	public void logout() {
		req.getSession().setAttribute("username", "");
		req.getSession().setAttribute("userid", "");
		
		Cookie[] cookies=req.getCookies();
		for(Cookie c:cookies)
		{
			String name=c.getName();
			c.setPath("/");
			if(name.equals("username"))
			{
				c.setMaxAge(0);
				rep.addCookie(c);
			}
			if(name.equals("userid"))
			{
				c.setMaxAge(0);
				rep.addCookie(c);
			}
		}
	}
	//����Ա�˳�
	public void adminLogout() {
		req.getSession().setAttribute("managername", "");
		req.getSession().setAttribute("managerId", "");
	}
	//�ͻ�ע��
	public void regist() {
		String username = req.getParameter("name");
		String password = req.getParameter("pwd");
		String idcard = req.getParameter("idcard");

		User user = null;
		user=dao.getUser(username);
		String result = "";
		if(user!=null)
		{
			result = username+" already exist";
		}
		else
		{
			user=new User();
			user.setIdcard(idcard);
			user.setPassword(password);
			user.setUsername(username);
	
			boolean flag = dao.addUser(user);
	
			if (!flag) {
				result = "false";
			} else {
				req.getSession().setAttribute("username", username);
				user=dao.getUser(username, password);
				if(user!=null)
				{
					req.getSession().setAttribute("userid", user.getId());
					result = "true";
					
					Cookie name_cookie = new Cookie("username",user.getUsername()); 
					name_cookie.setMaxAge(30*24*3600);
					name_cookie.setPath("/");
					rep.addCookie(name_cookie);
					Cookie id_cookie = new Cookie("userid",user.getId()+""); 
					id_cookie.setMaxAge(30*24*3600);
					id_cookie.setPath("/");
					rep.addCookie(id_cookie);
				}
				else
				{
					result = "false";
				}
				
			}
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//�ͻ��µ�
	public void order() {
		boolean flag = false;
		String cakeId = req.getParameter("cake");
		String cartId = req.getParameter("cart");
		String address = req.getParameter("address");
		String phone = req.getParameter("phone");
		String quantity = req.getParameter("quantity");
		String remark = req.getParameter("remark");
		String userId = req.getSession().getAttribute("userid").toString();

		OrderDao orderDao = new OrderDao();
		CakeDao cakeDao = new CakeDao();
		Order order = new Order();
		Cake cake = null;
		ShoppingCartDao cartDao=new ShoppingCartDao();
		ShoppingCart cart=null;
		if(cakeId!=null && !cakeId.equals(""))
		{
			cake=cakeDao.getCake(Long.parseLong(cakeId));
		}
		else if(cartId!=null && !cartId.equals(""))
		{
			cart=cartDao.getCart(Long.parseLong(cartId));
			if(cart!=null)
			{
				cake=cakeDao.getCake(cart.getCakeId());
			}
		}
		
		if(cake!=null)
		{
			order.setAddress(address);
			order.setAmount(cake.getPrice() * Integer.parseInt(quantity));
			order.setPhone(phone);
			order.setRemark(remark);
			order.setUserId(Long.parseLong(userId));
			if (orderDao.addOrder(order)) {
				//��������ɹ������涩����ϸ
				OrderDetailDao detailDao = new OrderDetailDao();
				OrderDetail detail = new OrderDetail();
				detail.setCakeId(cake.getId());
				Order temp = orderDao.getOrder(order.getCode());
				detail.setOrderId(temp.getId());
				detail.setQuantity(Integer.parseInt(quantity));
	
				flag = detailDao.addOrderDetail(detail);
			} else {
				//��������ʧ�ܣ�ɾ��ǰ����
				Order temp = orderDao.getOrder(order.getCode());
				orderDao.deleteOrder(temp);
			}
		}
		if(flag)
		{
			if(cart!=null)
			{
				cartDao.deleteCart(cart);
			}
		}
		String result = flag ? "true" : "false";
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//�ͻ�ȡ��
	public void cancelOrder() {
		String orderId = req.getParameter("order");
		String userId = req.getSession().getAttribute("userid").toString();
		String result = "false";
		if (userId == null || userId.equals("")) {
			result = "false";
		} else {
			OrderDao orderDao = new OrderDao();
			Order order = orderDao.getOrder(Long.parseLong(orderId));

			if (order != null) {
				order.setStatus(3);
				if (orderDao.updateOrder(order)) {
						result = "true";
				}
			}
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//�ͻ�ȡ��DIY����
	public void cancelDiyOrder() {
		String orderId = req.getParameter("order");
		String userId = req.getSession().getAttribute("userid").toString();
		String result = "false";
		if (userId == null || userId.equals("")) {
			result = "false";
		} else {
			DiyDao diyDao = new DiyDao();
			Diy diy = diyDao.getDiy(Long.parseLong(orderId));

			if (diy != null) {
				diy.setStatus(3);
				if (diyDao.updateDiy(diy)) {
						result = "true";
				}
			}
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//����Աȡ��
	public void cancelOrderByAdmin() {
		String orderId = req.getParameter("order");
		String managerId = req.getSession().getAttribute("managerid").toString();
		String result = "false";
		if (managerId == null || managerId.equals("")) {
			result = "false";
		} else {
			OrderDao orderDao = new OrderDao();
			Order order = orderDao.getOrder(Long.parseLong(orderId));

			if (order != null) {
				order.setStatus(3);
				if (orderDao.updateOrder(order)) {
						result = "true";
				}
			}
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//����Աȡ��DIY����
	public void cancelDiyOrderByAdmin() {
		String orderId = req.getParameter("order");
		String managerId = req.getSession().getAttribute("managerid").toString();
		String result = "false";
		if (managerId == null || managerId.equals("")) {
			result = "false";
		} else {
			DiyDao diyDao = new DiyDao();
			Diy diy = diyDao.getDiy(Long.parseLong(orderId));

			if (diy != null) {
				diy.setStatus(3);
				if (diyDao.updateDiy(diy)) {
						result = "true";
				}
			}
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//����Ա����
	public void sendOrder() {
		String orderId = req.getParameter("order");
		String managerId = req.getSession().getAttribute("managerid").toString();
		String result = "false";
		if (managerId == null || managerId.equals("")) {
			result = "false";
		} else {
			OrderDao orderDao = new OrderDao();
			Order order = orderDao.getOrder(Long.parseLong(orderId));

			if (order != null) {
				order.setStatus(1);
				if (orderDao.updateOrder(order)) {
						result = "true";
				}
			}
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//����Ա����
	public void sendDiyOrder() {
		String orderId = req.getParameter("order");
		String managerId = req.getSession().getAttribute("managerid").toString();
		String result = "false";
		if (managerId == null || managerId.equals("")) {
			result = "false";
		} else {
			DiyDao diyDao = new DiyDao();
			Diy diy = diyDao.getDiy(Long.parseLong(orderId));

			if (diy != null) {
				diy.setStatus(1);
				if (diyDao.updateDiy(diy)) {
						result = "true";
				}
			}
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//�ͻ�ȷ���ջ�
	public void confirmOrder() {
		String orderId = req.getParameter("order");
		String userid = req.getSession().getAttribute("userid").toString();
		String result = "false";
		if (userid == null || userid.equals("")) {
			result = "false";
		} else {
			OrderDao orderDao = new OrderDao();
			Order order = orderDao.getOrder(Long.parseLong(orderId));

			if (order != null) {
				order.setStatus(2);
				if (orderDao.updateOrder(order)) {
						result = "true";
				}
			}
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//�ͻ�ȷ���ջ�
	public void confirmDiyOrder() {
		String orderId = req.getParameter("order");
		String userid = req.getSession().getAttribute("userid").toString();
		String result = "false";
		if (userid == null || userid.equals("")) {
			result = "false";
		} else {
			DiyDao diyDao = new DiyDao();
			Diy diy = diyDao.getDiy(Long.parseLong(orderId));

			if (diy != null) {
				diy.setStatus(2);
				if (diyDao.updateDiy(diy)) {
						result = "true";
				}
			}
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//�ͻ��޸Ķ���
	public void updateOrder() {
		boolean flag = false;
		String orderId = req.getParameter("order");
		String address = req.getParameter("address");
		String phone = req.getParameter("phone");
		String remark = req.getParameter("remark");
		String userId = req.getSession().getAttribute("userid").toString();

		String result = "false";

		if (userId == null || userId.equals("")) {
			result = "false";
		} else {
			OrderDao orderDao = new OrderDao();
			Order order = orderDao.getOrder(Long.parseLong(orderId));
			if (order == null) {
				result = "false";
			} else {
				order.setAddress(address);
				order.setPhone(phone);
				order.setRemark(remark);
				flag = orderDao.updateOrder(order);

				result = flag ? "true" : "false";
			}
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void addToCart()
	{
		boolean flag = false;
		String cakeId = req.getParameter("cake");
		String num = req.getParameter("num");
		String userId = req.getSession().getAttribute("userid").toString();

		String result = "false";

		if (userId == null || userId.equals("")) {
			result = "false";
		} else {
			long userid=Long.parseLong(userId);
			long cakeid=Long.parseLong(cakeId);
			ShoppingCartDao dao = new ShoppingCartDao();
			ShoppingCart cart=dao.getCart(userid,cakeid );
			
			if(cart==null)
			{
				//add
				cart =new ShoppingCart();
				cart.setCakeId(cakeid);
				cart.setUserId(userid);
				cart.setQuantity(Integer.parseInt(num));
				flag = dao.addCart(cart);
			}
			else
			{
				cart.setQuantity(cart.getQuantity()+1);
				flag = dao.updateCart(cart);
			}
			
			if(flag)
			{
				result="true";
			}
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void addQ()
	{
		boolean flag = false;
		String cartId = req.getParameter("cart");
		String userId = req.getSession().getAttribute("userid").toString();

		String result = "false";

		if (userId == null || userId.equals("")) {
			result = "false";
		} else {
			long cartid=Long.parseLong(cartId);
			ShoppingCartDao dao = new ShoppingCartDao();
			ShoppingCart cart=dao.getCart(cartid);
			
			if(cart==null)
			{
				flag=false;
			}
			else
			{
				cart.setQuantity(cart.getQuantity()+1);
				flag = dao.updateCart(cart);
			}
			
			if(flag)
			{
				result="true";
			}
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void minusQ()
	{
		boolean flag = false;
		String cartId = req.getParameter("cart");
		String userId = req.getSession().getAttribute("userid").toString();

		String result = "false";

		if (userId == null || userId.equals("")) {
			result = "false";
		} else {
			long cartid=Long.parseLong(cartId);
			ShoppingCartDao dao = new ShoppingCartDao();
			ShoppingCart cart=dao.getCart(cartid);
			
			if(cart==null)
			{
				flag=false;
			}
			else
			{
				if(cart.getQuantity()<=1)
				{
					flag= dao.deleteCart(cart);
				}
				else
				{
					cart.setQuantity(cart.getQuantity()-1);
					flag = dao.updateCart(cart);
				}
				
			}
			
			if(flag)
			{
				result="true";
			}
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void deleteCart()
	{
		boolean flag = false;
		String cartId = req.getParameter("cart");
		String userId = req.getSession().getAttribute("userid").toString();

		String result = "false";

		if (userId == null || userId.equals("")) {
			result = "false";
		} else {
			long userid=Long.parseLong(userId);
			long cartid=Long.parseLong(cartId);
			ShoppingCartDao dao = new ShoppingCartDao();
			ShoppingCart cart=dao.getCart(cartid);
			
			if(cart==null)
			{
				flag=false;
			}
			else
			{
				flag=dao.deleteCart(cart);
			}
			
			if(flag)
			{
				result="true";
			}
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void orderAll()
	{
		
	}
	
	public void clearCart()
	{
		boolean flag = false;
		String userId = req.getSession().getAttribute("userid").toString();

		String result = "false";

		if (userId == null || userId.equals("")) {
			result = "false";
		} else {
			ShoppingCartDao cartDao = new ShoppingCartDao();
			long userid=Long.parseLong(userId);
			flag=cartDao.deleteAll(userid);
			
			if(flag)
			{
				result="true";
			}
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void diy()
	{
		boolean flag = false;
		String userId = req.getSession().getAttribute("userid").toString();

		String result = "false";

		if (userId == null || userId.equals("")) {
			result = "false";
		} else {
			DiyDao diyDao=new DiyDao();
			Diy diy=new Diy();
			String description = req.getParameter("description");
			String address = req.getParameter("address");
			String phone = req.getParameter("phone");
			String remark = req.getParameter("remark");
			String quantity=req.getParameter("quantity");
			float amount=Float.parseFloat(req.getParameter("amount"));
			
			diy.setAddress(address);
			diy.setAmount(amount);
			diy.setDescription(description);
			diy.setPhone(phone);
			diy.setQuantity(Integer.parseInt(quantity));
			diy.setRemark(remark);
			diy.setUserId(Long.parseLong(userId));
			
			String rootPath=req.getSession().getServletContext().getRealPath("/");
			String destPath=rootPath+"images\\require\\";
			List<String> files=UploadPicture.upload(req,destPath);
			if(files!=null){
				if(files.size()>0){
					String fileName=files.get(0);
					fileName=fileName.substring(fileName.lastIndexOf("\\")+1);
					
					diy.setPicture("images/require/"+fileName);
					
					flag=diyDao.addDiy(diy);
				}
			}
			if(flag)
			{
				result="<script>parent.toURL('myOrder.jsp');</script>";
			}
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void leaveMsg() {
		String msg = req.getParameter("msg");
		String userId = req.getSession().getAttribute("userid").toString();

		String result = "false";

		if (userId == null || userId.equals("")) {
			result = "false";
		} else {
			MessageDao msgDao = new MessageDao();
			Message message=new Message();
			message.setContent(msg);
			message.setUserid(Long.parseLong(userId));
			Date date=new Date();
			SimpleDateFormat sdFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			message.setOccurTime(sdFormat.format(date));
			
			if(msgDao.addMessage(message))
			{
				result="true";
			}
			
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void comment() {
		String comment = req.getParameter("comment");
		String orderId=req.getParameter("order");
		String userId = req.getSession().getAttribute("userid").toString();

		String result = "false";

		if (userId == null || userId.equals("")) {
			result = "false";
		} else {
			CommentDao cDao = new CommentDao();
			Comment c=new Comment();
			c.setContent(comment);
			c.setUserid(Long.parseLong(userId));
			c.setOrderid(Long.parseLong(orderId));
			Date date=new Date();
			SimpleDateFormat sdFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			c.setOccurTime(sdFormat.format(date));
			
			if(cDao.addComment(c))
			{
				result="true";
			}
			
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void deleteComment() {
		String commentId = req.getParameter("comment");
		String managerId = req.getSession().getAttribute("managerid").toString();
		String result = "false";
		if (managerId == null || managerId.equals("")) {
			result = "false";
		} else {
			CommentDao cDao = new CommentDao();
			Comment comment = cDao.getComment(Long.parseLong(commentId));

			if (comment != null) {
				if (cDao.deleteComment(comment)) {
						result = "true";
				}
			}
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void deleteCake() {
		String cakeId = req.getParameter("cake");
		String managerId = req.getSession().getAttribute("managerid").toString();
		String result = "false";
		if (managerId == null || managerId.equals("")) {
			result = "false";
		} else {
			CakeDao cDao = new CakeDao();
			Cake cake = cDao.getCake(Long.parseLong(cakeId));

			if (cake != null) {
				if (cDao.deleteCake(cake)) {
						result = "true";
				}
			}
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void editCake() {
		String cakeId = req.getParameter("id");
		String name=req.getParameter("name");
		String price=req.getParameter("price");
		//String categoryId=req.getParameter("categoryId");
		String categoryName=req.getParameter("categoryName");
		String valid=req.getParameter("valid");
		String remark=req.getParameter("remark");
		
		
		String managerId = req.getSession().getAttribute("managerid").toString();
		String result = "false";
		if (managerId == null || managerId.equals("")) {
			result = "false";
		} else {
			CakeDao cDao=new CakeDao();
			Cake cake=cDao.getCake(Long.parseLong(cakeId));
			if(cake!=null)
			{
				//cake.setCategoryId(Long.parseLong(categoryId));
				cake.setName(name);
				cake.setPrice(Float.parseFloat(price));
				cake.setRemark(remark);
				cake.setValid(Integer.parseInt(valid));
				
				String rootPath=req.getSession().getServletContext().getRealPath("/");
				String destPath=rootPath+"images\\"+categoryName+"\\";
				
				String oldPicture=cake.getPicture();
				oldPicture = oldPicture.replaceAll("/", "\\\\");
				List<String> files=UploadPicture.upload(req,destPath);
				boolean isNew=false;
				if(files!=null){
					if(files.size()>0){
						String fileName=files.get(0);
						fileName=fileName.substring(fileName.lastIndexOf("\\")+1);
						
						cake.setPicture("images/"+categoryName+"/"+fileName);
						isNew=true;
					}
				}
				if(cDao.updateCake(cake))
				{
					result="<script>parent.toURL('adminCakes.jsp');</script>";
					if(isNew)
					{
						if(FileUtil.deleteFile(rootPath+oldPicture))
						{
							System.out.println("delete file success:"+oldPicture);
						}
					}
				}
			}
			else
			{
				result="false";
			}
			
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void addCake() {
		String name=req.getParameter("name");
		String price=req.getParameter("price");
		String categoryId=req.getParameter("categoryId");
		String categoryName=req.getParameter("categoryName");
		String valid=req.getParameter("valid");
		String remark=req.getParameter("remark");
		
		
		String managerId = req.getSession().getAttribute("managerid").toString();
		String result = "false";
		if (managerId == null || managerId.equals("")) {
			result = "false";
		} else {
			CakeDao cDao=new CakeDao();
			Cake cake=new Cake();
			if(cake!=null)
			{
				cake.setCategoryId(Long.parseLong(categoryId));
				cake.setName(name);
				cake.setPrice(Float.parseFloat(price));
				cake.setRemark(remark);
				cake.setValid(Integer.parseInt(valid));
				
				String rootPath=req.getSession().getServletContext().getRealPath("/");
				String destPath=rootPath+"images\\"+categoryName+"\\";
				List<String> files=UploadPicture.upload(req,destPath);
				if(files!=null){
					if(files.size()>0){
						String fileName=files.get(0);
						fileName=fileName.substring(fileName.lastIndexOf("\\")+1);
						
						cake.setPicture("images/"+categoryName+"/"+fileName);
					}
				}
				if(cDao.addCake(cake))
				{
					result="<script>parent.toURL('adminCakes.jsp');</script>";
				}
			}
			else
			{
				result="false";
			}
			
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void updatePicture() {
		String diyId = req.getParameter("id");
		
		
		String userId = req.getSession().getAttribute("userid").toString();
		String result = "false";
		if (userId == null || userId.equals("")) {
			result = "false";
		} else {
			
			DiyDao dDao=new DiyDao();
			Diy diy=dDao.getDiy(Long.parseLong(diyId));
			if(diy!=null)
			{
				String rootPath=req.getSession().getServletContext().getRealPath("/");
				String destPath=rootPath+"images\\require\\";
				
				String oldPicture=diy.getPicture();
				oldPicture = oldPicture.replaceAll("/", "\\\\");
				List<String> files=UploadPicture.upload(req,destPath);
				boolean isNew=false;
				if(files!=null){
					if(files.size()>0){
						String fileName=files.get(0);
						fileName=fileName.substring(fileName.lastIndexOf("\\")+1);
						
						diy.setPicture("images/require/"+fileName);
						isNew=true;
					}
				}
				if(dDao.updateDiy(diy))
				{
					result="<script>parent.toURL('myOrder.jsp');</script>";
					if(isNew)
					{
						if(FileUtil.deleteFile(rootPath+oldPicture))
						{
							System.out.println("delete file success:"+oldPicture);
						}
					}
				}
			}
			else
			{
				result="false";
			}
			
		}
		try {
			PrintWriter out = rep.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
